#pragma once

#include <iostream>
#include <string>
#include <vector>
#include <ctemplate/template.h>

#include "../comm/log.hpp"
#include "../comm/util.hpp"
//#include "./oj_model.hpp"
#include "./oj_model_mysql.hpp"

namespace ns_view
{
    using namespace ns_log;
    using namespace ns_util;
    //using namespace ns_model;
    using namespace ns_model_mysql;
    class View
    {
    private:
        static std::string viewhtmlpath;

    public:
        View()
        {
        }
        ~View()
        {
        }

    public:
        /*********************
         * 功能：渲染题目列表
         *********************/
        void AllExpandHtml(const std::vector<Question> &questions, std::string *html)
        {
            // 1、构建路径
            std::string file = viewhtmlpath + "allquestions.html";

            // 2、创建数据字典
            ctemplate::TemplateDictionary root("allquestions");

            // 3、构建分枝插入数据
            for (const auto &q : questions)
            {
                ctemplate::TemplateDictionary *sub = root.AddSectionDictionary("questionlist");
                sub->SetValue("number", q.number); // 题目编号
                sub->SetValue("title", q.title);   // 题目名称
                sub->SetValue("star", q.star);     // 题目难度
            }

            // 4、获取被渲染的html
            ctemplate::Template *tql = ctemplate::Template::GetTemplate(file, ctemplate::DO_NOT_STRIP);

            // 5、开启渲染功能
            tql->Expand(html, &root);
        }

        void OneExpandHtml(const Question &q, std::string *html)
        {
            // 1、构建路径
            std::string file = viewhtmlpath + "onequestion.html";

            // 2、创建数据字典
            ctemplate::TemplateDictionary root("onequestion");

            root.SetValue("number", q.number);
            root.SetValue("title", q.title);
            root.SetValue("star", q.star);
            root.SetValue("desc", q.desc);
            root.SetValue("header", q.header);

            // 3、获取被渲染的html
            ctemplate::Template *tql = ctemplate::Template::GetTemplate(file, ctemplate::DO_NOT_STRIP);

            // 4、开启渲染功能
            tql->Expand(html, &root);
        }
    };
    std::string View::viewhtmlpath = "./questions.html/";
}